PyAnime
!pip install -U gif
!pip install "gif[altair]"
!pip install "gif[matplotlib]"
!pip install "gif[plotly]"
# !apt-get update # to update ubuntu to correctly run apt install
# !apt install chromium-chromedriver
# !cp /usr/lib/chromium-browser/chromedriver /usr/bin
# import sys
# sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
# from selenium import webdriver
# chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--no-sandbox')
# chrome_options.add_argument('--disable-dev-shm-usage')
# wd = webdriver.Chrome('chromedriver', options=chrome_options)
import random
from matplotlib import pyplot as plt
import gif
x = [random.randint(0, 100) for _ in range(100)]
y = [random.randint(0, 100) for _ in range(100)]
gif.options.matplotlib["dpi"] = 300
@gif.frame
def plot(i):
xi = x[i*10:(i+1)*10]
yi = y[i*10:(i+1)*10]
plt.scatter(xi, yi)
plt.xlim((0, 100))
plt.ylim((0, 100))
frames = []
for i in range(10):
frame = plot(i)
frames.append(frame)
gif.save(frames, 'example.gif', duration=3.5, unit="s", between="startend")
from IPython.display import Image
Image(open('example.gif','rb').read(), width = 400, height = 300)
import random
import altair as alt
import pandas as pd
import gif
df = pd.DataFrame({
't': list(range(10)) * 10,
'x': [random.randint(0, 100) for _ in range(100)],
'y': [random.randint(0, 100) for _ in range(100)]
})
@gif.frame
def plot(i):
d = df[df['t'] == i]
chart = alt.Chart(d).encode(
x=alt.X('x', scale=alt.Scale(domain=(0, 100))),
y=alt.Y('y', scale=alt.Scale(domain=(0, 100)))
).mark_circle()
return chart
frames = []
for i in range(10):
frame = plot(i)
frames.append(frame)
gif.save(frames, 'example2.gif', duration=100, unit="ms", between="frames")
from IPython.display import Image
Image(open('example2.gif','rb').read(), width = 400, height = 300)
import random
import plotly.graph_objects as go
import pandas as pd
import gif
df = pd.DataFrame({
't': list(range(10)) * 10,
'x': [random.randint(0, 100) for _ in range(100)],
'y': [random.randint(0, 100) for _ in range(100)]
})
@gif.frame
def plot(i):
d = df[df['t'] == i]
fig = go.Figure()
fig.add_trace(go.Scatter(
x=d["x"],
y=d["y"],
mode="markers"
))
fig.update_layout(width=500, height=300)
return fig
frames = []
for i in range(10):
frame = plot(i)
frames.append(frame)
gif.save(frames, 'example3.gif', duration=100)
from IPython.display import Image
Image(open('example3.gif','rb').read(), width = 400, height = 300)
import altair as alt
from altair import Chart, X, Y, Color, Scale, Axis
import pandas as pd
import gif
df = pd.read_csv('https://covid.ourworldindata.org/data/ecdc/full_data.csv')
df['date'] = pd.to_datetime(df['date'])
select = df[df['location'].isin([
'United States',
'China',
'Canada',
'Sweden']
)].reset_index(drop=True)
population = {
'United States': 330_000_000,
'China': 1_440_000_000,
'Canada': 37_000_000,
'Sweden': 10_000_100
}
select['population'] = select['location'].map(population)
select['cases_per_million'] = select.apply(lambda row: row['new_cases'] / row['population'] * 1_000_000, axis=1)
select['7d'] = select.groupby('location').rolling(7)['cases_per_million'].mean().reset_index(drop=True)
@gif.frame
def plot(date):
d = select[select['date'] <= date]
xmin = d['date'].min()
delta = (d['date'].max() - xmin).days
tmax = d['date'].max() + pd.Timedelta(days=max(30, delta))
xmax = min(tmax, d['date'].max()) + pd.Timedelta(days=5)
ymax = max(5, d['7d'].max()) + 5
date_ticks = pd.date_range(xmin, pd.Timestamp('now'), freq='MS').tolist()
chart = Chart(d).encode(
x=X("date",
title=None,
scale=Scale(domain=(xmin, xmax)),
axis=Axis(format="%b", values=date_ticks)
),
y=Y('7d', title=None, scale=Scale(domain=(0, ymax))),
color=Color('location',
scale=Scale(
domain=['United States', 'China', 'Canada', 'Sweden'],
range=["#000055", "#550000", "#881111", "#2222CC"]
),
legend=None
)
).mark_line().properties(width=500, height=300)
return chart
dates = pd.date_range(start='2020-02-01', end=df['date'].max())
frames = []
for date in dates:
frame = plot(date)
frames.append(frame)
gif.save(frames, 'covid.gif', duration=75)
from IPython.display import Image
Image(open('covid.gif','rb').read(), width = 400, height = 300)